<?php
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

function modgetCatTree(&$tree,$directory) {
	global $database;

	$q = "SELECT c.*, ".
	"\n (select count(b.id) ".
	"\n  from #__boss_".$directory."_content_category_href as a, ".
    "\n  #__boss_".$directory."_contents as b ".
	"\n  where a.category_id = c.id ".
    "\n  and b.id = a.content_id ".
	"\n    and b.published = 1 ) ".
	"\n as num_contents ".
	"\n FROM #__boss_".$directory."_categories as c ".
	"\n WHERE c.published = 1 ".
	"\n ORDER BY c.parent,c.ordering";

    $database->setQuery($q);
	$list = $database->loadObjectList();
						 
	// establish the hierarchy of the menu
	$tree = array();
	// first pass - collect children
	if(isset($list))
	{
		foreach ($list as $v ) {
			$pt 	= $v->parent;
			$list_temp 	= @$tree[$pt] ? $tree[$pt] : array();
			array_push( $list_temp, $v );
			$tree[$pt] = $list_temp;
		}
	}
}


$directory = intval($params->get( 'directory', mosGetParam( $_GET, 'directory', 1 ) )) ;
$option = "com_boss";
$catid          = intval( mosGetParam( $_GET, 'catid', 0 ) );
require_once( JPATH_BASE .'/components/com_boss/boss.html.php' );
require_once( JPATH_BASE .'/components/com_boss/boss.class.php' );
include_once( JPATH_BASE .'/components/com_boss/lang/russian.php' );

// get configuration
$database->setQuery( "SELECT * FROM #__boss_config WHERE id=$directory");
$database->loadObject($conf);
if ($database -> getErrorNum()) {
	echo $database -> stderr();
	return false;
}

$template_name = $conf->template;

$mainframe = mosMainFrame::getInstance(true);

$mainframe->addCSS( JPATH_SITE.'/templates/com_boss/'.$template_name.'/css/boss.css');

$bossHtmlClass = new boss_html();

// Dynamic Page Title
$mainframe->SetPageTitle( BOSS_PAGE_TITLE . BOSS_ADVANCED_SEARCH );

$database->setQuery( "SELECT f.* FROM #__boss_".$directory."_fields AS f ".
					 "WHERE f.searchable = 1 AND f.published = 1 AND profile = 0" );

$fields_searchable = $database->loadObjectList();
if ($database->getErrorNum()) {
	echo $database->stderr();
	return;
}

//get value fields
$database->setQuery( "SELECT * FROM #__boss_".$directory."_field_values ORDER by ordering ");
$fieldvalues = $database->loadObjectList();
if ($database -> getErrorNum()) {
	echo $database -> stderr();
	return false;
}

$field_values = array();
// first pass - collect children
if (isset($fieldvalues))
{
	foreach ($fieldvalues as $v ) {
		$pt 	= $v->fieldid;
		$list 	= @$field_values[$pt] ? $field_values[$pt] : array();
		array_push( $list, $v );
		$field_values[$pt] = $list;
	}
}

$itemid = intval( mosGetParam( $_GET, 'Itemid', 0 ));
$paths = null;
$paths[0]->text = BOSS_ROOT_TITLE;
$paths[0]->link = sefRelToAbs('index.php?option='.$option.'&amp;directory='.$directory.'&amp;Itemid='.$itemid);
$bossHtmlClass->paths  = $paths;

modgetCatTree($cats,$directory);
global $my;

$bossHtmlClass->user = $my;
$bossHtmlClass->conf = $conf;
$bossHtmlClass->option = $option;
$bossHtmlClass->itemid = $itemid;
$bossHtmlClass->fields = $fields_searchable;
$bossHtmlClass->field_values = $field_values;
$bossHtmlClass->category->id = $catid;
$bossHtmlClass->categories = $cats;
$bossHtmlClass->directory = $directory;
$bossHtmlClass->template_name = $template_name;
$bossHtmlClass->displaySearch($option,$fields_searchable,$field_values,$catid,$cats,$itemid);
	
?>